import { Component, View, CORE_DIRECTIVES } from 'angular2/angular2';
import { Router } from 'angular2/router';

import { status, text } from '/resources/ts/fetch.ts'

@Component({
  selector: 'wrapper',
  templateUrl: '/resources/template/main.html',
  directives: [CORE_DIRECTIVES]
})
export class Main {
  jwt: string;
  decodedJwt: string;
  response: string;
  api: string;

  constructor(public router: Router) {
    this.jwt = localStorage.getItem('jwt');
    // this.decodedJwt = this.jwt && window.jwt_decode(this.jwt);

    document.getElementsByTagName('body')[0].className = 'hold-transition skin-blue sidebar-mini';

  }

  logout() {
    localStorage.removeItem('jwt');
    this.router.parent.navigateByUrl('/login');
  }

  // callAnonymousApi() {
  //   this._callApi('Anonymous', 'http://localhost:3001/api/random-quote');
  // }
  //
  // callSecuredApi() {
  //   this._callApi('Secured', 'http://localhost:3001/api/protected/random-quote');
  // }
  //
  // _callApi(type, url) {
  //   this.response = null;
  //   this.api = type;
  //   window.fetch(url, {
  //     method: 'GET',
  //     headers: {
  //       'Accept': 'application/json',
  //       'Content-Type': 'application/json',
  //       'Authorization': 'bearer ' + this.jwt
  //     }
  //   })
  //     .then(status)
  //     .then(text)
  //     .then((response) => {
  //     this.response = response;
  //   })
  //     .catch((error) => {
  //     this.response = error.message;
  //   });
  // }
}
